//
//  HMPictureView.swift
//  WeiboTT
//
//  Created by 王嘉涛 on 2016/11/17.
//  Copyright © 2016年 王嘉涛. All rights reserved.
//

import UIKit
private let cellId = "HMPictureCellId"

class HMPictureView: UICollectionView {
    
    var pictureInfos: [HMPictureInfo]? {
        didSet {
            //外界给配图视图设置数据的时候就会执行
            self.reloadData()
        }
    }
    
    override func awakeFromNib() {
        //注册cell
        let pictureCellNib = UINib(nibName: "HMPictureCell", bundle: nil)
        self.register(pictureCellNib, forCellWithReuseIdentifier: cellId)
        
        //显示数据 遵守dataSource协议 实现数据方法
        self.dataSource = self
    }
    
}


extension HMPictureView: UICollectionViewDataSource {
    //实现数据源方法
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HMPictureCell
        cell.pictureInfo = self.pictureInfos![indexPath.row]
        return cell
    }
}
